/*
 * DominoSprite is a Sprite subclass.
 */ 
DominoSprite.prototype = new Sprite();

function DominoSprite(context, x, y, width, height, clickCallback, hoverCallback, topPips, bottomPips) {
				
	Sprite.call(this, context, x, y, width, height, clickCallback, null);
		
	this.isTurnedUp = true;	
	this.pipRadius = 0.12 * width;	

	this.imageContext.lineWidth = 0.85 * this.pipRadius;
	this.image.width += 2 * this.imageContext.lineWidth;
	this.image.height += 2 * this.imageContext.lineWidth;
						
	this.margins = this.getMargins();			
	
	this.initDrawPipsFunctions(topPips, bottomPips);		

	this.initImage();
}

DominoSprite.prototype.initImage = function () {	

	this.drawTile();
	
	if(this.isTurnedUp) {
		this.drawLine();
		this.drawTopPips2();					
		this.drawBottomPips2();
	}
}	

DominoSprite.prototype.drawLine = function() {
	
	this.imageContext.strokeStyle = "#000000";
	this.imageContext.lineWidth = 0.85 * this.pipRadius;

	var margins = this.getMargins();		
	this.imageContext.strokeLine(margins.x, 
											margins.y + 0.5 * this.height,
											margins.x + this.width,
											margins.y + 0.5 * this.height);
}		
DominoSprite.prototype.drawTile = function() {
																			
		this.imageContext.fillStyle = "#FFCC77";
						
		if(this.isHovered) {									
			this.imageContext.fillStyle = "#6577EB";
		}
		
		this.imageContext.strokeStyle = "#000000";
		this.imageContext.lineWidth = 0.85 * this.pipRadius;		
		
		// Centralize drawing on canvas				
		var margins = this.getMargins();		
		
		this.imageContext.fillRoundRect(margins.x, margins.y,
											this.width,
											this.height, 
											10);
		
		this.imageContext.strokeRoundRect(margins.x, margins.y,
												this.width, this.height,
												10);			
}

DominoSprite.prototype.initDrawPipsFunctions = function(topPips, bottomPips) {
	if(topPips == 0) {			
					
		this.drawTopPips2 = function() {								
									
		}
	
	}	
	if(topPips == 1) {			
					
		this.drawTopPips2 = function() {								
			
			var pipPos = new Vector2(0.5 * this.width,
												0.5 * this.width);
			
			pipPos.translate(this.margins);
											
			this.imageContext.fillStyle = "#000000";
			this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius);				
		}
	
	}

	if(topPips == 2) {

		this.drawTopPips2 = function() {
			this.imageContext.fillStyle = "#000000";
		
			for(var offsetCoefficient = 0.25; offsetCoefficient <= 0.75; offsetCoefficient += 0.5) {		
				
				var pipPos = new Vector2(offsetCoefficient * this.width,
													offsetCoefficient * this.width);
					
				pipPos.translate(this.margins);
																				
				this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius);							
			}
		}		
	}

	if(topPips == 3) {
		this.drawTopPips2 = function() {
			
			this.imageContext.fillStyle = "#000000";

			for(var offsetCoefficient = 0.25; offsetCoefficient <= 0.75; offsetCoefficient += 0.25) {		
				
				var pipPos = new Vector2(offsetCoefficient * this.width,
													offsetCoefficient * this.width);				
				pipPos.translate(this.margins);
				
				this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius);
			}
		}
	}
	
	if(topPips == 4) {
		
		this.drawTopPips2 = function() {
			
			this.imageContext.fillStyle = "#000000";
			
			for(var offsetCoefficientX = 0.25; offsetCoefficientX <= 0.75; offsetCoefficientX += 0.5) {
				for(var offsetCoefficientY = 0.25; offsetCoefficientY <= 0.75; offsetCoefficientY += 0.5) {		
					
					var pipPos = new Vector2(offsetCoefficientX * this.width,
														offsetCoefficientY * this.width);
					pipPos.translate(this.margins);
					
					this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius, 0, 2 * Math.PI);								
				}
			}
		}
	
	}

	if(topPips == 5) {
		
		this.drawTopPips2 = function() {

			var pipPos = new Vector2(0.5 * this.width, 0.5 * this.width);
			pipPos.translate(this.margins);
			
			this.imageContext.fillStyle = "#000000"; 						
			this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius, 0, 2 * Math.PI);								
			
			for(var offsetCoefficientX = 0.25; offsetCoefficientX <= 0.75; offsetCoefficientX += 0.5) {
				for(var offsetCoefficientY = 0.25; offsetCoefficientY <= 0.75; offsetCoefficientY += 0.5) {		
					
					var pipPos = new Vector2(offsetCoefficientX * this.width,
														offsetCoefficientY * this.width);				
					pipPos.translate(this.margins);
					
					this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius);								
				}
			}
		
		}
	}
	
	if(topPips == 6) {
		
		this.drawTopPips2 = function() {
		
			this.imageContext.fillStyle = "#000000";
			
			var pipPos = new Vector2(0.5 * this.width, 0.5 * this.width);
			pipPos.translate(this.margins); 																							
			for(var offsetCoefficientX = 0.25; offsetCoefficientX <= 0.75; offsetCoefficientX += 0.5) {				
				for(var offsetCoefficientY = 0.25; offsetCoefficientY <= 0.75; offsetCoefficientY += 0.25) {		
				
					var pipPos = new Vector2(offsetCoefficientX * this.width, offsetCoefficientY * this.width);
					pipPos.translate(this.margins);														
					this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius, 0, 2 * Math.PI);								
				}
			
			}		
		}
	}

	if(bottomPips == 0) {
		
		this.drawBottomPips2 = function() {}													
	}

	if(bottomPips == 1) {
		
		this.drawBottomPips2 = function() {
		
			this.imageContext.fillStyle = "#000000";
				
			var pipPos = new Vector2(0.5 * this.width, 0.5 * this.width); 			
			pipPos.y += this.width;
			pipPos.translate(this.margins);
								
			this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius);
		}											
	}

	if(bottomPips == 2) {
		this.drawBottomPips2 = function() {
			this.imageContext.fillStyle = "#000000";
				
			for(var offsetCoefficient = 0.25; offsetCoefficient <= 0.75; offsetCoefficient += 0.5) {		
				
				var pipPos = new Vector2(offsetCoefficient * this.width,
													offsetCoefficient * this.width);
				pipPos.y += this.width;
				pipPos.translate(this.margins);
				this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius);							
			}		
		}
	}

	if(bottomPips == 3) {
		this.drawBottomPips2 = function () {
		
			this.imageContext.fillStyle = "#000000";
			
			for(var offsetCoefficient = 0.25; offsetCoefficient <= 0.75; offsetCoefficient += 0.25) {		
				
				var pipPos = new Vector2(offsetCoefficient * this.width,
													offsetCoefficient * this.width);				
				
				pipPos.y += this.width;
				pipPos.translate(this.margins);
				this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius);									
			}
		
		}	
	}

	if(bottomPips == 4) {
		this.drawBottomPips2 = function() {

			this.context.fillStyle = "#000000";
			
			for(var offsetCoefficientX = 0.25; offsetCoefficientX <= 0.75; offsetCoefficientX += 0.5) {
				for(var offsetCoefficientY = 0.25; offsetCoefficientY <= 0.75; offsetCoefficientY += 0.5) {
							
					var pipPos = new Vector2(offsetCoefficientX * this.width,
														offsetCoefficientY * this.width);
					pipPos.y += this.width;
					pipPos.translate(this.margins);													
					this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius, 0, 2 * Math.PI);								
				}
			}
		
		}
	}
				
	if(bottomPips == 5) {
		this.drawBottomPips2 = function() {
			
			this.imageContext.fillStyle = "#000000";
				
			var pipPos = new Vector2(0.5 * this.width, 
												0.5 * this.width);
			pipPos.y += this.width;
			pipPos.translate(this.margins);
			this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius);								
			
			for(var offsetCoefficientX = 0.25; offsetCoefficientX <= 0.75; offsetCoefficientX += 0.5) {
				for(var offsetCoefficientY = 0.25; offsetCoefficientY <= 0.75; offsetCoefficientY += 0.5) {		

					var pipPos = new Vector2(offsetCoefficientX * this.width,
														offsetCoefficientY * this.width);				
					pipPos.y += this.width;
					pipPos.translate(this.margins);										
					this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius, 0, 2 * Math.PI);								
				}
			}
		
		}
	}

	if(bottomPips == 6) {
				
		this.drawBottomPips2 = function () {
		this.imageContext.fillStyle = "#000000";
						
			var pipPos = new Vector2(0.5 * this.width, 0.5 * this.width);
			pipPos.y += this.width;
			pipPos.translate(this.margins); 																						
			 
			for(var offsetCoefficientX = 0.25; offsetCoefficientX <= 0.75; offsetCoefficientX += 0.5) {
				for(var offsetCoefficientY = 0.25; offsetCoefficientY <= 0.75; offsetCoefficientY += 0.25) {		
					var pipPos = new Vector2(offsetCoefficientX * this.width,
												offsetCoefficientY * this.width);
					pipPos.y += this.width;
					pipPos.translate(this.margins);				
										
					this.imageContext.fillCircle(pipPos.x, pipPos.y, this.pipRadius);								
				}
			}
		
		}
	}
}

DominoSprite.prototype.setTurnedUp = function(up) {
	if(up !=  this.isTurnedUp) {
		this.isTurnedUp = up;		
		this.initImage();
	}	
}

DominoSprite.prototype.getMargins = function() {
	var marginX = 0.5 * (this.image.width - this.width);
	var marginY = 0.5 * (this.image.height - this.height);
	
	return new Vector2(marginX, marginY);
}